﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;

using DevExpress.XtraEditors;

namespace SZ
{
    public partial class ImportControlPointFrm : Form
    {
        public ImportControlPointFrm()
        {
            InitializeComponent();
            this.richTextBox1.Text = "点号" + "\n";
        }

        private void buttonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            OpenFileDialog _openFiledialog = new OpenFileDialog();
            _openFiledialog.ShowDialog();
            buttonEdit1.Text = _openFiledialog.FileName;
            // 将gridControl的数据源设置为连接的Excel表格
            this.gridControl1.DataSource = ExcelToDataTable(buttonEdit1.Text, "Sheet1");
            this.gridView1.BestFitColumns();
        }

        private void ImportBtn_Click(object sender, EventArgs e)
        {
            string ExcelFileName = buttonEdit1.Text;
            DataTable _dataTable = ExcelToDataTable(ExcelFileName, "Sheet1");

            if (_dataTable == null)
            {
                MessageBox.Show("请选择一个Excel文件！", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }

            for (int i = 1; i < _dataTable.Rows.Count; i++)
            {
                //InsertDataToSDE(_dataTable.Rows[i][0].ToString(), float.Parse(_dataTable.Rows[i][1].ToString()));
                InsertDataToSDE(_dataTable, i,this.richTextBox1);
                
            }
            this.buttonEdit1.Text = "";
            this.gridControl1.DataSource = null;

        }

        private static DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)
        {

            //数据源的定义
            string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
            //sql语句
            string strExcel = string.Format("select *from [{0}$]", strSheetName);
            //定义存放的数据点
            DataSet ds = new DataSet();
            //连接数据源
            OleDbConnection conn = new OleDbConnection(strCon);
            try
            {
                conn.Open();
                //适配到数据源
                OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strCon);
                adapter.Fill(ds, strSheetName);
                conn.Close();
            }
            catch
            { }
            return ds.Tables[strSheetName];

        }

        public static void InsertDataToSDE(DataTable dataTable, int i,RichTextBox richTextBox)
        {

            IWorkspace pWorkspace;
            ConnectSDE connectSDE = new ConnectSDE();
            pWorkspace = connectSDE.SdeWorkspace();

            IFeatureWorkspace pFeatureWorkspace;
            pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass pFeatureClass;
            pFeatureClass = pFeatureWorkspace.OpenFeatureClass("SDE.ControlPoints");

            ITable pTable;
            pTable = pFeatureClass as ITable;
            IRow pRow;
            pRow = pTable.CreateRow();
            for (int t = 0; t < 9; t++)
            {
                int k = t + 1;
                if (k == 9)
                {
                    IPoint pPoint = new PointClass();
                    pPoint.PutCoords(Convert.ToDouble(pRow.get_Value(1)), Convert.ToDouble(pRow.get_Value(2)));
                    pRow.set_Value(9, pPoint);
                }
                string  a= dataTable.Rows[i][t ].ToString ();
                pRow.set_Value(k, dataTable.Rows[i][t]);
            }
            pRow.Store();
            MessageBox.Show("导入成功！");
            richTextBox.AppendText(pRow.get_Value(1) + "\n");
        }
    
        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
